package com.workstudy.service.impl;

import com.workstudy.domain.Arrange;
import com.workstudy.domain.Course;
import com.workstudy.mapper.ArrangeMapper;
import com.workstudy.mapper.CourseMapper;
import com.workstudy.model.ArrangeModel;
import com.workstudy.service.ArrangeService;
import zhibi.common.support.base.service.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * The class create by 执笔 on generate.
 */
@Service
@Transactional
public class ArrangeServiceImpl extends BaseServiceImpl<Arrange> implements ArrangeService {

    @Autowired
    public ArrangeServiceImpl(ArrangeMapper mapper) {
        super(mapper);
        this.arrangeMapper = mapper;
    }

    private ArrangeMapper arrangeMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public List<ArrangeModel> selectModel() {
        return arrangeMapper.selectModel();
    }

    @Override
    public void init() {
        /**
         * 先删除以前的
         */
        arrangeMapper.deleteAll();
        /**
         * 得到所有的课程
         */
        List<Course> courseList = courseMapper.selectAll();

        //
        Map<Integer, String> monday = new HashMap<>();
        Map<Integer, String> tuesday = new HashMap<>();
        Map<Integer, String> wednesday = new HashMap<>();
        Map<Integer, String> thursday = new HashMap<>();
        Map<Integer, String> friday = new HashMap<>();

        for (Course course : courseList) {
            monday.put(course.getUserId(), course.getMonday());
            tuesday.put(course.getUserId(), course.getTuesday());
            wednesday.put(course.getUserId(), course.getWednesday());
            thursday.put(course.getUserId(), course.getThursday());
            friday.put(course.getUserId(), course.getFriday());
        }

        plan(monday);
        plan(tuesday);
        plan(wednesday);
        plan(thursday);
        plan(friday);

    }

    /**
     * 安排
     *
     * @param map
     */
    private void plan(Map<Integer, String> map) {
        List<Integer> cahe = new ArrayList<>();
        Arrange arrange = new Arrange();
        for (int i = 0; i < 5; i++) {
            Iterator<Integer> iterator = map.keySet().iterator();
            Integer userId = 0;
            while (iterator.hasNext()) {
                Integer temp = iterator.next();
                String str = map.get(temp);
                if (str.substring(i, i + 1).equals("0")) {//没课
                    userId = temp;
                    if (!cahe.contains(userId)) {
                        break;
                    }
                }
            }
            cahe.add(userId);
            if (i == 0) arrange.setOne(userId);
            if (i == 1) arrange.setTwo(userId);
            if (i == 2) arrange.setThree(userId);
            if (i == 3) arrange.setFour(userId);
            if (i == 4) arrange.setFive(userId);
        }
        arrangeMapper.insertSelective(arrange);
    }

    public static void main(String[] args) {
        System.out.println("12345".substring(4, 5));
    }
}